package com.moneywelcome.emails;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * 发送邮件工具
 */
public class EmailUtil {

    public static void sendMail(String sender,String reciver,String title,String content,String password) throws MessagingException {
        Properties properties = new Properties();
        properties.setProperty("mail.transport.protocol","SMTP");
        properties.setProperty("mail.host","smtp.163.com");
        properties.setProperty("mail.smtp.auth","true");

        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                //pop3的授权码设置
                return new PasswordAuthentication(sender, password);
            }
        });
        Message message = new MimeMessage(session);
        //设置发送者
        message.setFrom(new InternetAddress(sender));
        //设置接受者
        message.setRecipient(Message.RecipientType.TO,new InternetAddress(reciver));
        //设置邮件标题
        message.setSubject(title);
        //设置内容
        message.setContent(content,"text/html;charset=utf-8");
        //发送
        Transport.send(message);
    }
}
